﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SuperStore.Models;

namespace SuperStore.Controllers
{
    public class CartController : SuperStoreController
    {
        public ActionResult Index()
        {
            return ViewCart();
        }

        private ActionResult ViewCart()
        {
            if (this.ValidMemberID)
            {
                List<CartItem> cart = Orders.GetCart(this.MemberID);

                return View(cart);
            }
            else
            {
                return RedirectToAction("Login", "Profile");
            }
        }

        [HttpPost]
        public ActionResult GetCartTotals()
        {
            List<CartItem> cart = Orders.GetCart(this.MemberID);
            KeyValuePair<Int64, decimal> cartTotals = CalculateCartTotals(cart);            
            return Json(new { TotalPoints = cartTotals.Key, TotalCost = cartTotals.Value });
        }

        private KeyValuePair<Int64, decimal> CalculateCartTotals(List<CartItem> cart)
        {            
            Int64 TotalPoints = 0;
            decimal TotalCost = 0;
            foreach (CartItem cartItem in cart)
            {
                TotalPoints += cartItem.MSPoints * cartItem.Quantity;
                TotalCost += cartItem.RetailCost * cartItem.Quantity;
            }

            return new KeyValuePair<Int64, decimal>(TotalPoints, TotalCost);
        }

        public ActionResult Checkout()
        {
            List<CartItem> cart = Orders.GetCart(this.MemberID);
            KeyValuePair<Int64, decimal> cartTotals = CalculateCartTotals(cart);

            ViewData["Total Cost"] = cartTotals.Value;
            ViewData["Total Points"] = cartTotals.Key;
            return ViewCart();
        }

        public ActionResult Add(string gameID)
        {
            if (this.ValidMemberID)
            {
                Orders.Add(this.MemberID, gameID);
                return RedirectToAction("Index");
            }
            else
            {
                return RedirectToAction("Login", "Profile");
            }
        }

        public ActionResult Remove(Guid ID)
        {
            Orders.Remove(ID);
            return RedirectToAction("Index");
        }

        public ActionResult ConfirmCheckout()
        {
            if (this.ValidMemberID)
            {
                Orders.CheckOut(this.MemberID);
            }

            return View();
        }

        [HttpPost]
        public ActionResult UpdateCart(long quantity, string orderItemId)
        {
            Orders.ChangeQuantity(Guid.Parse(orderItemId), quantity);
                        
            return Json("Done");
        }

        [HttpPost]
        public ActionResult GetUpdatedCostDetails(string orderItemId)
        {
            Guid orderItemGUID = Guid.Parse(orderItemId);
            OrderItem item = (from o in Database.Instance.OrderItems
                              where o.PK.Equals(orderItemGUID)
                              select o).Single();

            Game game = (from g in Database.Instance.Games
                         where g.PK.Equals(item.GameID)
                         select g).Single();

            string itemCost = string.Empty;
            string totalItemCost = string.Empty;

            if (game.Arcade)
            {
                itemCost = string.Format("{0} points", item.MSPointsPerItem);
                totalItemCost = string.Format("{0} points", item.MSPointsPerItem * item.Amount);
            }
            else
            {
                itemCost = string.Format("{0:F}", item.CostPerItem);
                totalItemCost = string.Format("{0:F}", item.CostPerItem * item.Amount);
            }
            
            return Json(new { ItemCost = itemCost, TotalItemCost = totalItemCost, });
        }
    }
}
